package com.esyoga.yoyoga.handler;

import com.esyoga.yoyoga.pojo.TContract;
import com.esyoga.yoyoga.pojo.TOrder;
import com.esyoga.yoyoga.service.ContractService;
import com.esyoga.yoyoga.service.OrderService;
import com.esyoga.yoyoga.util.CutPagesUtil;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@RequestMapping("/order")
@Controller
@Transactional
public class OrderHandler {

    @Resource
    OrderService orderService;
    @Resource
    ContractService contractService;

    //存储预返回页面的数据对象
    private Map<String, Object> result = new HashMap<>();

    /**
     * 根据合约ID和订单状态查询订单
     * @param page
     * @param oState 订单状态：0：为完成，1：已完成
     * @param cId 合约ID
     * @return
     */
    @PostMapping("/findOrders")
    @ResponseBody
    public Map<String, Object> selectOrdersByContractId(Integer page, Integer cId, Integer oState){
        List<TOrder> orders = orderService.selectOrdersByContractId(cId,oState);

        CutPagesUtil.clear();
        CutPagesUtil.setPageInfo(page,10);
        List<TOrder> orderList = CutPagesUtil.getPages(orders);
        int totalPage = CutPagesUtil.getTotalPage();

        result.put("orders",orderList);
        result.put("total",totalPage);
        return result;
    }


    /**
     * 创建订单
     * @param cId：需要的参数：合约cId
     * @return
     */
    @RequestMapping("/addOrder")
    @ResponseBody
    public Map<String, Object> createOrder(Integer cId){
        //根据合约ID查找合约订单
        TContract contract = contractService.selectByCId(cId);
        if(contract==null){
            result.put("result","failed");
            result.put("reason","合约不存在");
            return result;
        }
        BigDecimal orderMoney  = contract.getcBanlance();//订单金额
        BigDecimal platMoney = orderMoney.multiply(new BigDecimal(contract.getcGaPlatformratio()));//平台分成金额
        BigDecimal gymMoney = orderMoney.multiply(new BigDecimal(contract.getcTaGymratio()));//场馆分成金额
        BigDecimal trainerMoney = orderMoney.subtract(platMoney).subtract(gymMoney);//教练分成金额
        Random random = new Random();
        String orderNum = Long.toString(new Date().getTime())+random.nextInt(10)+random.nextInt(10)+random.nextInt(10);//订单号


        TOrder tOrder = new TOrder();
        tOrder.setoCId(cId);
        tOrder.setoBanlance(orderMoney);
        tOrder.setoTrainerbanlance(trainerMoney);
        tOrder.setoGymbanlance(gymMoney);
        tOrder.setoPlatformbanlance(platMoney);
        tOrder.setoNum(orderNum);
        tOrder.setoState(0);

        int res = orderService.insertOrder(tOrder);
        if(res>0){
            result.put("result","success");
        }else {
            result.put("result","failed");
        }
        return result;
    }

    /**
     * 更新订单：此更新方法根据主键找到对应的订单，然后tContract中不为null的属性
     * 即如果需要更新某个字段，传入主键和需要更新的属性值就可以了
     * @param tOrder 订单
     * @return
     */
    @RequestMapping("/updateOrder")
    @ResponseBody
    public Map<String, Object> updateOrder(TOrder tOrder){
        int res = orderService.updateOrder(tOrder);
        if(res>0){
            result.put("result","success");
        }else {
            result.put("result","failed");
        }
        return result;
    }


    /**
     * 支付订单
     * @param oId
     * @return
     */
    @RequestMapping("/payOrder")
    @ResponseBody
    public Map<String, Object> payOrder(Integer oId){
        if(oId==null){
            result.put("result","fail");
            result.put("reason","缺少参数");
            return result;
        }

        return orderService.payOrder(oId);

    }

}
